package cn.bj.ykct.spring.data;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
public class DataSourceConfig {


	@Bean(name = "testDS")
	@Qualifier("testDS")
	@Primary
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource etcDataSource(
			@Value("${spring.datasource.driverClassName}") String driver,
			@Value("${spring.datasource.url}") String url, 
			@Value("${spring.datasource.username}") String username,
			@Value("${spring.datasource.password}") String password) {

		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setDriverClassName(driver);
		druidDataSource.setUrl(url);
		druidDataSource.setUsername(username);
		druidDataSource.setPassword(password);
		druidDataSource.setValidationQuery("SELECT 'x'");
		druidDataSource.setTestOnBorrow(false);
		druidDataSource.setTestWhileIdle(true);

		try {
			druidDataSource.setFilters("stat, wall");
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return druidDataSource;
	}
}
